package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Book;
import com.example.entity.Params;
import com.example.entity.Type;
import com.example.mapper.BookMapper;
import com.example.mapper.TypeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class BookService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private TypeMapper typeMapper;


    public PageInfo<Book> findBySearch(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页设置来查询
        List<Book> list = bookMapper.findBySearch(params);
//        if (CollectionUtil.isEmpty(list)) {
//            return PageInfo.of(new ArrayList<>());
//        }
//        for (Book book : list) {
//            if (ObjectUtil.isNotEmpty(book.getTypeId())) {
//                Type type = typeMapper.selectByPrimaryKey(book.getTypeId());
//                if (ObjectUtil.isNotEmpty(type)) {
//                    book.setTypeName(type.getName());
//                }
//            }
//        }
        return PageInfo.of(list);
    }

    public void add(Book book) {
        bookMapper.insertSelective(book);
    }

    public void update(Book book) {
        bookMapper.updateByPrimaryKeySelective(book);
    }

    public void delete(Integer id) {
        bookMapper.deleteByPrimaryKey(id);
    }

    public List<Book> findAll() {
        return bookMapper.selectAll();
    }

    public List<Book> findAlls() {
        return bookMapper.findAlls();
    }
}
